package com.love.marriage.carouselBannerInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.carouselBannerInfo.entity.CarouselBannerInfo;
import com.love.marriage.carouselBannerInfo.mapper.CarouselBannerInfoMapper;
import com.love.marriage.carouselBannerInfo.service.CarouselBannerInfoService;
import com.love.marriage.carouselBannerInfo.vo.CarouselBannerInfoVo;
import com.love.marriage.oss.service.FileInfoService;
import com.love.marriage.oss.vo.FileInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  展示轮播图表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-20 10:10:50
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class CarouselBannerInfoServiceImp extends ServiceImpl<CarouselBannerInfoMapper, CarouselBannerInfo> implements CarouselBannerInfoService {

    private final CarouselBannerInfoMapper carouselBannerInfoMapper;

    private final FileInfoService fileInfoService;

    @Override
    public Page<CarouselBannerInfoVo> getPage(Long pageNum, Long pageSize, CarouselBannerInfoVo carouselBannerInfoVo) {
        Page<CarouselBannerInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        Page<CarouselBannerInfoVo> res = carouselBannerInfoMapper.getPage(page, carouselBannerInfoVo);
        if (res != null && res.getRecords() != null && res.getRecords().size() > 0) {
            res.getRecords().forEach(file -> {
                try {
                    file.setFileInfoVo(fileInfoService.queryFileInfo(file.getFileId()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        return res;
    }

    @Override
    public List<CarouselBannerInfoVo> list(CarouselBannerInfoVo carouselBannerInfoVo) {
        if (carouselBannerInfoVo == null) {
            carouselBannerInfoVo = new CarouselBannerInfoVo();
        }
        carouselBannerInfoVo.setStatus(true);
        List<CarouselBannerInfoVo> list = carouselBannerInfoMapper.list(carouselBannerInfoVo);
        if (list != null && list.size() > 0) {
            list.forEach(res -> {
                try {
                    res.setFileInfoVo(fileInfoService.queryFileInfo(res.getFileId()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        return list;
    }

    @Override
    public CarouselBannerInfoVo queryCarouselBannerInfo(Long id) {
        CarouselBannerInfoVo carouselBannerInfoVo = carouselBannerInfoMapper.queryCarouselBannerInfo(id);
        if (carouselBannerInfoVo != null && carouselBannerInfoVo.getFileId() != null) {
            try {
                carouselBannerInfoVo.setFileInfoVo(fileInfoService.queryFileInfo(carouselBannerInfoVo.getFileId()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return carouselBannerInfoVo;
    }

    @Override
    public CarouselBannerInfoVo addCarouselBannerInfo(String title, MultipartFile file) throws Exception {
        CarouselBannerInfoVo carouselBannerInfoVo = new CarouselBannerInfoVo();
        carouselBannerInfoVo.setTitle(title);
        FileInfoVo fileInfoVo = fileInfoService.addFileInfo("common", file);
        carouselBannerInfoVo.setFileId(fileInfoVo.getId());
        CarouselBannerInfo carouselBannerInfo = new CarouselBannerInfo();
        BeanUtils.copyProperties(carouselBannerInfoVo, carouselBannerInfo);
        carouselBannerInfoMapper.insert(carouselBannerInfo);
        CarouselBannerInfoVo res = new CarouselBannerInfoVo();
        BeanUtils.copyProperties(carouselBannerInfo, res);
        return res;
    }

    @Override
    public CarouselBannerInfoVo addCarouselBannerInfo(CarouselBannerInfoVo carouselBannerInfoVo) throws Exception {
        CarouselBannerInfo carouselBannerInfo = new CarouselBannerInfo();
        BeanUtils.copyProperties(carouselBannerInfoVo, carouselBannerInfo);
        if (carouselBannerInfoVo.getStatus() == null) {
            carouselBannerInfo.setStatus(true);
        }
        carouselBannerInfoMapper.insert(carouselBannerInfo);
        CarouselBannerInfoVo res = new CarouselBannerInfoVo();
        BeanUtils.copyProperties(carouselBannerInfo, res);
        return res;
    }

    @Override
    public CarouselBannerInfoVo updateCarouselBannerInfo(CarouselBannerInfoVo carouselBannerInfoVo) {
        CarouselBannerInfo carouselBannerInfo = new CarouselBannerInfo();
        BeanUtils.copyProperties(carouselBannerInfoVo, carouselBannerInfo);
        carouselBannerInfoMapper.updateById(carouselBannerInfo);
        return carouselBannerInfoVo;
    }

    @Override
    public Boolean deleteCarouselBannerInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        carouselBannerInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
